package com.song.provider.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.song.provider.entity.Book;
import com.song.provider.fallback.SentinelWebController;
import com.song.provider.sentinel.BookSentinel;
import com.song.provider.service.BookService;
import com.song.provider.service.FeignSerivce;
import com.song.provider.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 宋伟宁
 * @Date 2024/1/9
 * @Version 1.0
 **/
@RestController
public class WebController {

    @Resource
    private BookService bookService;


    @GetMapping("/books")
    public List<Book> books() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return this.bookService.getBookList();
    }



    @SentinelResource(blockHandler = "areaPoint",value = "findById")
    @GetMapping("/{id}")
    public Book findById(@PathVariable("id") long id){
        return this.bookService.findById(id);
    }


    public Book areaPoint(@PathVariable("id") long id){
        Book book =new Book();
        book.setTitle("热点熔断！");
        return  book;
    }
//    @Resource
//    private FeignSerivce feignSerivce;
//
//    @Resource
//    private UserService userService;
//
//    @GetMapping("/server1")
//    public String server1(){
//        userService.getUser();
//        return "server1访问";
//    }
//    @GetMapping("/server2")
//    public String server2(){
//        userService.getUser();
//        return "server2访问";
//    }
//
//    @Value("${server.port}")
//    private String port;
//
//
//
//   // @SentinelResource(fallback = "fallback",fallbackClass = BookSentinel.class)
//    //BookSentinel.fallback()
//    @GetMapping("/")
//    public String list() {
//        return this.feignSerivce.list() + this.port;
//
//    }
//
//   // @SentinelResource(fallback = "fallbackAdd",fallbackClass = BookSentinel.class)
//    //BookSentinel.fallback()
//    @GetMapping("/add")
//    public String add() {
//        return this.feignSerivce.add() + this.port;
//
//    }
//   // @SentinelResource(fallback = "fallbackUpdate",fallbackClass = BookSentinel.class)
//    //BookSentinel.fallback()
//    @GetMapping("/update")
//    public String update() {
//        return this.feignSerivce.update() + this.port;
//
//    }
//    //@SentinelResource(fallback = "fallbackDelete",fallbackClass = BookSentinel.class)
//    //BookSentinel.fallback()
//    @GetMapping("/delete")
//    public String delete() {
//        return this.feignSerivce.delete() + this.port;
//
//    }

}

